package net.ssta.core.db;

import java.util.ArrayList;
import java.util.List;

import net.ssta.core.code.JsonEncodeAdapter;

/**
 * 
 * @author qiuzhl
 */
public class RecordSet 
{
	private List<Record> m_records = new ArrayList<Record>();
	private String m_entity_name ;
	private JsonEncodeAdapter m_adapter = null;
	private String m_raw_sql = null;
	
	/**
	 * ��Recordʵ����ӵ���ݼ�RecordSet��
	 * @param record
	 */
	public void add(Record record)
	{
		m_records.add(record);
	}
	
	/**
	 * ������RecordSet�е������ӵ�����ݼ��У���ʱû��ʵ��
	 * @param recordSet
	 */
	public void add(RecordSet recordSet)
	{
		this.m_records.addAll(recordSet.m_records);
	}
	
	/**
	 * ����±�ȡ����ݼ��е�һ��recordʵ������±�Ϊ�������ߴ�����ݼ��ĳ��ȣ��򷵻�NULL
	 * @param index record �� RecordSet �е��±�
	 * @return
	 */
	public Record get(int index)
	{
		return m_records.get(index);
	}
	
	/**
	 * ȡ����ݼ��Ĵ�С
	 * @return
	 */
	public int size()
	{
		return m_records.size();
	}
	
	/**
	 * ��ָ���±�ļ�¼����ݼ�RecordSet��ɾ������±�Ϊ�������ߴ�����ݼ��ĳ��ȣ��򲻽����κβ���
	 * @param index
	 */
	public void remove(int index)
	{
		if(index >= 0 && index < m_records.size() )
		{
			m_records.remove(index);
		}		
	}
	
	/**
	 * ��Recordʵ�����ݼ�RecordSet��ɾ�������Ƴ�ļ�¼�����ڣ��򲻽����κβ���
	 * @param index
	 */
	public void remove(Record record)
	{
		m_records.remove(record);
	}
	
	/**
	 * ��ʱûʵ��
	 * @return
	 */
	public String getEntityName()
	{
		if(m_entity_name == null && m_records.size() > 0)
		{
			m_entity_name = m_records.get(0).getEntityName();
		}
		return m_entity_name;
	}
	
	/**
	 * ��ʱûʵ��
	 * @param entityName
	 */
	public void setEntityName(String entityName)
	{
		m_entity_name = entityName;
	}
	
	/**
	 * ���û�ȡJSON��ʽ���ʱʹ�õı�����
	 * @param adapter
	 */
	public void setJsonEncoder(JsonEncodeAdapter adapter)
	{
		m_adapter = adapter;
	}
	
	/**
	 * ��ʱûʵ��
	 * @param xml
	 */
	public void setXmlString(String xml)
	{
		
	}
	
	/**
	 * ��ʱûʵ��
	 * @return
	 */
	public String getXmlString()
	{
		return null;
	}
	
	public String getJsonString()
	{
		if(m_adapter != null) return getJsonString(m_adapter);
		StringBuffer sb = new StringBuffer();
		sb.append("[ ");
		for(int i = 0; i<m_records.size(); i++)
		{
			sb.append(m_records.get(i).getJsonString()).append(",");
		}
		sb.setCharAt(sb.length()-1, ']');
		return sb.toString();
	}
	
	public String getJsonString(JsonEncodeAdapter adapter)
	{
		StringBuffer sb = new StringBuffer();
		sb.append("[ ");
		for(int i = 0; i<m_records.size(); i++)
		{
			sb.append(m_records.get(i).getJsonString(adapter)).append(",");
		}
		sb.setCharAt(sb.length()-1, ']');
		return sb.toString();
	}
	
	public String getRawSQL()
	{
		return m_raw_sql == null ? "" : m_raw_sql;
	}
	
	protected void setRawSQL(String sql)
	{
		m_raw_sql = sql;
	}
}
